package io.demo;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.Map;
import java.util.function.Supplier;

class ChromeDriverPath implements Supplier<String> {

    private static final Map<String, String> PATH_CONFIG;

    static final ChromeDriverPath SINGLE = new ChromeDriverPath();

    @Override
    public String get() {
        String os = System.getProperty("os.name");
        return PATH_CONFIG.entrySet()
                .stream()
                .filter(x -> StringUtils.lowerCase(os).contains(x.getKey()))
                .findAny()
                .orElseThrow(IllegalArgumentException::new)
                .getValue();
    }

    static {
        final String fmt = "src/main/resources/selenium/drivers/chrome/{0}/chromedriver{1}";
        PATH_CONFIG = new ImmutableMap.Builder<String, String>()
                .put("linux", MessageFormat.format(fmt, "linux64", StringUtils.EMPTY))
                .put("mac", MessageFormat.format(fmt, "mac", StringUtils.EMPTY))
                .put("win", MessageFormat.format(fmt, "win32", ".exe"))
                .build();
    }
}
